#
# Kamaemlia component for translating English to LOLspeak
#
# Copyright (c)2008 Stephen Newey
# Licensed under the Mozilla Public License v1.1.
# http://www.mozilla.org/MPL/MPL-1.1.html
#

from Axon.Component import component
from Axon.Ipc import shutdownMicroprocess, producerFinished
from lolz import Tranzlator

class LolzTranslator(component):
    Inboxes = {"inbox"  : "Sentence to translate",
                "control"   : "Stop component", }
    Outboxes = {"outbox"    : "Translated sentence", 
                "signal"    : "Shutdown signal", }
    
    def __init__(self, **kwargs):
        super(LolzTranslator, self).__init__(**kwargs)
        self.translator = Tranzlator()
    
    def main(self):
        while not self.doShutdown():
            if self.dataReady("inbox"):
                sentence = self.recv("inbox")
                self.send(self.translator.translate_sentence(sentence), "outbox")
            if not self.anyReady():
                self.pause()
            yield 1
    
    def doShutdown(self):
        if self.dataReady("control"):
            message = self.recv("control")
            if isinstance(message, shutdownMicroprocess) or isinstance(message, producerFinished):
                self.send(producerFinished(), "signal")
                return True
        return False

